package com.coffee.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.platform.query.CarouselFigureQuery;
import com.coffee.admin.api.platform.result.CarouselFigureResult;
import com.coffee.admin.api.platform.service.CarouselFigureService;
import com.coffee.core.enums.SkuSalesInformationTypeEnum;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.order.api.sku.service.PeriodizationSkuService;
import com.coffee.sku.api.sku.service.*;
import com.coffee.web.controller.BaseController;
import com.coffee.core.anotation.LoginCheck;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.result.Result;
import com.coffee.sku.api.sku.result.*;
import com.coffee.sku.api.sku.req.MerchantSkuListReq;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/merchant")
public class SkuController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(SkuController.class);
    @DubboReference(check = false)
    private CarouselFigureService carouselFigureService;
    @DubboReference(check = false)
    private SkuCategoryService skuCategoryService;
    @DubboReference(check = false)
    private SkuService skuService;
    @DubboReference(check = false)
    private SkuContentService skuContentService;
    @DubboReference(check = false)
    private SkuShopcarService skuShopcarService;
    @DubboReference(check = false)
    private RecommendSkuService recommendSkuService;
    @DubboReference(check = false)
    private PeriodizationSkuService periodizationSkuService;

    @OperLog(operModul = "商户-首页轮播图", operDesc = "首页轮播图列表")
    @RequestMapping(value = "/rotation/chart", method = {RequestMethod.POST})
    public Result<List<CarouselFigureResult>> findList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<List<CarouselFigureResult>> result = Result.success(null);
        Integer cityId = jsonObject.getInteger("cityId");
        CarouselFigureQuery carouselFigureQuery = new CarouselFigureQuery();
        carouselFigureQuery.setIsEnable("Y");
        carouselFigureQuery.setCityId(cityId);
        result.setData(carouselFigureService.getList(carouselFigureQuery));
        return result;
    }

    @OperLog(operModul = "商户-类目类别", operDesc = "类目类别列表")
    @RequestMapping(value = "/category/list", method = {RequestMethod.POST})
    public Result<List<SkuCategoryResult>> categoryList(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Result<List<SkuCategoryResult>> result = Result.success(null);
        String isOnlyFirst = jsonObject.getString("isOnlyFirst");
        Integer cityId = jsonObject.getInteger("cityId");
        result.setData(skuCategoryService.getListByCity(isOnlyFirst, cityId));
        return result;
    }


    @OperLog(operModul = "商户-商户搜索记录", operDesc = "商户搜索记录列表")
    @RequestMapping(value = "/search/list", method = {RequestMethod.POST})
    public Result<Set<SearchListResult>> searchList(HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Result<Set<SearchListResult>> result = Result.success(null);
        if (merchant!=null){
            result.setData(skuService.getSearchList(merchant.getUserId()));
        }
        return result;
    }

    @OperLog(operModul = "商户-清空记录", operDesc = "清空记录")
    @LoginCheck
    @RequestMapping(value = "/delete/search", method = {RequestMethod.POST})
    public Result<String> deleteSearch(HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Result<String> result = Result.success(null);
        result.setData(skuService.deleteSearch(merchant.getUserId()));
        return result;
    }


    @OperLog(operModul = "商户-商品列表", operDesc = "商品列表")
    @RequestMapping(value = "/sku/list", method = {RequestMethod.POST})
    public Result<Page<MerchantSkuListResult>> skuList(@RequestBody MerchantSkuListReq req, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        if (merchant != null && StringUtils.isNotBlank(req.getSkuName())) {
            skuService.addSearchList(merchant.getUserId(), req.getSkuName());
        }
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        Result<Page<MerchantSkuListResult>> result = Result.success(null);
        req.setSkuType(SkuTypeEnum.SKU.getCode());
        req.setIsDisable("N");
        req.setIsEnable("Y");
        req.setIsApprove("Y");
        req.setIsDelete("N");
        req.setIsDelete("N");
        req.setType(SkuSalesInformationTypeEnum.GENERAL.getCode());
        result.setData(skuContentService.findMerchantSkuList(req, pagerInfo));
        return result;
    }


    @OperLog(operModul = "商户-商品详情", operDesc = "商品详情")
    @RequestMapping(value = "/sku/detail", method = {RequestMethod.POST})
    public Result<MerchantSkuContentResult> skuDetail(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Integer id = jsonObject.getInteger("id");
        Integer cityId = jsonObject.getInteger("cityId");
        String type = jsonObject.getString("type");
        Result<MerchantSkuContentResult> result = Result.success(null);
        try {
            if (StringUtils.isBlank(type)) {
                type = SkuSalesInformationTypeEnum.GENERAL.getCode();
            }
            result.setData(skuContentService.getMerchantSkuContentResult(id, merchant!=null?merchant.getUserId():null, cityId, type));
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }


    @OperLog(operModul = "商户-购物车列表", operDesc = "购物车列表")
    @LoginCheck
    @RequestMapping(value = "/sku/car/list", method = {RequestMethod.POST})
    public Result<List<SkuCarListResult>> skuCarList(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Integer cityId = jsonObject.getInteger("cityId");
        Result<List<SkuCarListResult>> result = Result.success(null);
        result.setData(skuShopcarService.getSkuShopcarResult(merchant.getUserId(), cityId));
        return result;
    }


    @OperLog(operModul = "商户-加入购物车", operDesc = "加入购物车")
    @LoginCheck
    @RequestMapping(value = "/sku/add/car", method = {RequestMethod.POST})
    public Result<String> skuAddCar(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginUser merchant = getMerchant();
        Integer skuId = jsonObject.getInteger("skuId");
        Integer skuSalesInformationId = jsonObject.getInteger("skuSalesInformationId");
        Integer count = jsonObject.getInteger("count");
        Result<String> result = Result.success(null);
        SkuShopcarResult skuShopcarResult = new SkuShopcarResult();
        skuShopcarResult.setMerchantId(merchant.getUserId());
        skuShopcarResult.setSkuId(skuId);
        skuShopcarResult.setSkuSalesInformationId(skuSalesInformationId);
        skuShopcarResult.setCount(count);
        skuShopcarResult.setCreateTime(new Date());
        try {
            skuShopcarService.addOrUpdate(skuShopcarResult);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "商户-删除购物车", operDesc = "删除购物车")
    @LoginCheck
    @RequestMapping(value = "/sku/delete/car", method = {RequestMethod.POST})
    public Result<String> skuDeleteCar(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Result<String> result = Result.success(null);
        LoginUser merchant = getMerchant();
        JSONArray id = jsonObject.getJSONArray("id");
        List<Integer> integers = JSONObject.parseArray(JSONObject.toJSONString(id), Integer.class);
        skuShopcarService.delete(integers, merchant.getUserId());
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "商户-修改购物车", operDesc = "删除购物车")
    @LoginCheck
    @RequestMapping(value = "/sku/update/car", method = {RequestMethod.POST})
    public Result<String> skuUpdateCar(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Result<String> result = Result.success(null);
        LoginUser merchant = getMerchant();
        Integer id = jsonObject.getInteger("id");
        Integer count = jsonObject.getInteger("count");
        SkuShopcarResult skuShopcarResult = new SkuShopcarResult();
        skuShopcarResult.setId(id);
        skuShopcarResult.setMerchantId(merchant.getUserId());
        skuShopcarResult.setCount(count);
        try {
            skuShopcarService.addOrUpdate(skuShopcarResult);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "商户-商品列表", operDesc = "商品列表")
    @RequestMapping(value = "/recommend/sku/list", method = {RequestMethod.POST})
    public Result<List<MerchantSkuListResult>> recommendSkuList(@RequestBody MerchantSkuListReq req, HttpServletRequest request) {
        Result<List<MerchantSkuListResult>> result = Result.success(null);
        req.setSkuType(SkuTypeEnum.SKU.getCode());
        req.setIsDisable("N");
        req.setIsEnable("Y");
        req.setIsApprove("Y");
        req.setIsDelete("N");
        List<MerchantSkuListResult> merchantSkuListResult = recommendSkuService.getMerchantSkuListResult(req);
        result.setData(merchantSkuListResult);
        return result;
    }

    @OperLog(operModul = "商户-分期购商品列表", operDesc = "分期购商品列表")
    @RequestMapping(value = "/periodization/sku/list", method = {RequestMethod.POST})
    public Result<List<MerchantSkuListResult>> periodizationSkuList(@RequestBody MerchantSkuListReq req, HttpServletRequest request) {
        Result<List<MerchantSkuListResult>> result = Result.success(null);
        req.setSkuType(SkuTypeEnum.SKU.getCode());
        req.setIsDisable("N");
        req.setIsEnable("Y");
        req.setIsApprove("Y");
        req.setIsDelete("N");
        List<MerchantSkuListResult> merchantSkuListResult = periodizationSkuService.getMerchantSkuListResult(req);
        result.setData(merchantSkuListResult);
        return result;
    }

}
